﻿/**
 * @file fwave_wpcm_test1.cpp
 * Copyright (c) Gaaagaa. All rights reserved.
 * 
 * @author  : Gaaagaa
 * @date    : 2020-02-17
 * @version : 1.0.0.0
 * @brief   : 录制 wave-pcm 格式的音频文件（测试 fwave_wpcm_t）。
 */

#include "fwave_pcm.h"
#include "audio_dev.h"

#define SDL_main_h_
#include <SDL/SDL.h>

#include <vector>
#include <thread>
#include <utility>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

////////////////////////////////////////////////////////////////////////////////

int main(int argc, char * argv[])
{
    x_errno_t xerr_no = X_ERR_UNKNOW;
    x_int32_t xit_err = X_ERR_UNKNOW;

    //======================================

    if (argc < 2)
    {
        printf("Usage: \n\t %s < wave-pcm file path >\n", argv[0]);
        return -1;
    }

    SDL_Init(SDL_INIT_AUDIO);

    //======================================

    x_uint32_t xut_frequency = 48000;
    x_uint16_t xut_sampbits  = 16;
    x_uint8_t  xut_channels  = 2;

    fwave_wpcm_t   fwave_wpcm;
    adev_capture_t audio_capture;

    //======================================

    xerr_no = fwave_wpcm.open(
                    argv[1],
                    xut_frequency,
                    xut_sampbits,
                    xut_channels);
    if (XERR_FAILED(xerr_no))
    {
        printf("fwave_wpcm.open([%s]) return error code: %s\n",
               argv[1], fwave_wpcm_t::xerrno_name(xerr_no));
        return -1;
    }

    //======================================

    xit_err = audio_capture.open(
                    -1,
                    xut_channels,
                    X_AUDIO_S16,
                    xut_frequency);
    if (0 != xit_err)
    {
        printf("audio_capture.open(-1, [%d], [%d], [%d]) return error code: %d\n",
               xut_channels,
               X_AUDIO_S16,
               xut_frequency,
               xit_err);
        fwave_wpcm.close();
        return -1;
    }

    printf("capture:\n\tfrequency:%6d\n\tchannels :%6d\n\tformat   :%6d\n",
           audio_capture.audio_spec().xut_frequency,
           audio_capture.audio_spec().xut_channels,
           audio_capture.audio_spec().xut_format);

    //======================================

    x_bool_t xbt_continue = X_TRUE;
    std::thread xthread(
        [&xbt_continue, &fwave_wpcm, &audio_capture](void) -> void
        {
            x_int32_t xit_rbytes = 0;
            std::vector< x_byte_t > xbuffer(1024 * 1024, 0);
            while (xbt_continue)
            {
                xit_rbytes =
                    audio_capture.capture(
                        xbuffer.data(), (x_uint32_t)xbuffer.size());

                if (xit_rbytes > 0)
                {
                    fwave_wpcm.write(xbuffer.data(), xit_rbytes);
                }

                std::this_thread::sleep_for(std::chrono::milliseconds(40));
            }
        });

    //======================================

    printf("audio capture running......, press any key to stop!\n");
    system("pause");

    xbt_continue = X_FALSE;

    if (xthread.joinable())
    {
        xthread.join();
    }

    //======================================

    audio_capture.close();
    fwave_wpcm.close();

    SDL_Quit();

    //======================================

    return 0;
}
